package net.es.oscars.authZ.http.policy;

import java.util.List;


import org.apache.log4j.Logger;
import org.hibernate.Session;

import net.es.oscars.authCommonPolicy.soap.gen.AttrDetails;
import net.es.oscars.authCommonPolicy.soap.gen.ListAttrsReply;
import net.es.oscars.authCommonPolicy.soap.gen.ModifyAttrDetails;
import net.es.oscars.authZ.beans.Attribute;
import net.es.oscars.authZ.beans.Authorization;
import net.es.oscars.authZ.beans.Constraint;
import net.es.oscars.authZ.beans.Permission;
import net.es.oscars.authZ.beans.Resource;
import net.es.oscars.authZ.beans.Rpc;
import net.es.oscars.authZ.common.AuthZCore;
import net.es.oscars.authZ.common.AuthZException;
import net.es.oscars.authZ.common.PolicyManager;
import net.es.oscars.authZ.soap.gen.policy.AuthDetails;
import net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType;
import net.es.oscars.authZ.soap.gen.policy.ConstraintDetails;
import net.es.oscars.authZ.soap.gen.policy.ListAuthsParams;
import net.es.oscars.authZ.soap.gen.policy.ListAuthsReply;
import net.es.oscars.authZ.soap.gen.policy.ListConstraintsReply;
import net.es.oscars.authZ.soap.gen.policy.ListPermissionsReply;
import net.es.oscars.authZ.soap.gen.policy.ListResourcesReply;
import net.es.oscars.authZ.soap.gen.policy.ListRpcsReply;
import net.es.oscars.authZ.soap.gen.policy.ModifyAuthDetails;
import net.es.oscars.authZ.soap.gen.policy.PermissionDetails;
import net.es.oscars.authZ.soap.gen.policy.ResourceDetails;
import net.es.oscars.authZ.soap.gen.policy.RpcDetails;
import net.es.oscars.common.soap.gen.EmptyArg;
import net.es.oscars.common.soap.gen.OSCARSFaultMessage;
import net.es.oscars.utils.soap.OSCARSFaultUtils;

/**
 * This class was generated by Apache CXF 2.2.2
 * Tue Jun 30 15:40:53 PDT 2009
 * Generated source version: 2.2.2
 *
 */

@javax.jws.WebService(
    serviceName = "AuthZPolicyService",
    portName = "AuthZPolicyPort",
    targetNamespace = "http://oscars.es.net/OSCARS/authZPolicy",
    endpointInterface = "net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType")
@javax.xml.ws.BindingType(value = "http://www.w3.org/2003/05/soap/bindings/HTTP/")
public class AuthZPolicySoapHandler implements AuthZPolicyPortType {

    private static final Logger LOG = Logger.getLogger(AuthZPolicySoapHandler.class);
    private AuthZCore core = AuthZCore.getInstance();

    /* (non-Javadoc)
     * @see net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType#addAuth(net.es.oscars.authZ.soap.gen.policy.AuthDetails  addAuthReqMsg )*
     */
    public EmptyArg addAuth(AuthDetails addAuthReqMsg)
            throws OSCARSFaultMessage {

        String method = "authZ/common/PolicyManager:addAuth";
        LOG.debug("addAuth.start");
        EmptyArg reply = new EmptyArg();
        PolicyManager mgr = core.getPolicyManager();
        Session session = core.getSession();
        try {
            session.beginTransaction();
            String attributeValue = addAuthReqMsg.getAttributeValue();
            String resourceName = addAuthReqMsg.getResourceName();
            String permissionName = addAuthReqMsg.getPermissionName();
            String constraintName = addAuthReqMsg.getConstraintName();
            String constraintValue = addAuthReqMsg.getConstraintValue();
            mgr.createAuthorization(attributeValue, resourceName, permissionName,
                                    constraintName, constraintValue);
        } catch (AuthZException ex) {
            OSCARSFaultUtils.handleError ( ex, true, session, LOG, method);
        } catch (Exception ex) {
            OSCARSFaultUtils.handleError ( ex, false, session, LOG, method);
        }
        session.getTransaction().commit();
        LOG.debug("addAuth.finish");
        return reply;
    }

    /* (non-Javadoc)
     * @see net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType#listAuths(net.es.oscars.authZ.soap.gen.policy.ListAuthsParams  listAuthsReqMsg )*
     */
    public ListAuthsReply listAuths(ListAuthsParams listAuthsReqMsg)
            throws OSCARSFaultMessage {

        String method = "authZ/common/PolicyManager:listAuths";
        LOG.debug("listAuths.start");
        ListAuthsReply reply = new ListAuthsReply();
        PolicyManager mgr = core.getPolicyManager();
        Session session = core.getSession();
        try {
            session.beginTransaction();
            String attributeValue = listAuthsReqMsg.getAttribute();
            List<Authorization> auths = mgr.listAuths(attributeValue);
            for (Authorization auth: auths) {
                AuthDetails authDetails = this.authToAuthReply(auth);
                reply.getAuthDetails().add(authDetails);
            }
        } catch (AuthZException ex) {
            OSCARSFaultUtils.handleError ( ex, true, session, LOG, method);
        } catch (Exception ex) {
            OSCARSFaultUtils.handleError ( ex, false, session, LOG, method);
        }
        session.getTransaction().commit();
        LOG.debug("listAuths.finish");
        return reply;
    }

    /* (non-Javadoc)
     * @see net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType#modifyAuth(net.es.oscars.authZ.soap.gen.policy.ModifyAuthDetails  modifyAuthReqMsg )*
     */
    public EmptyArg modifyAuth(ModifyAuthDetails modifyAuthReqMsg)
            throws OSCARSFaultMessage {

        String method = "authZ/common/PolicyManager:modifyAuth";
        LOG.debug("modifyAuth.start");
        EmptyArg reply = new EmptyArg();
        PolicyManager mgr = core.getPolicyManager();
        Session session = core.getSession();
        try {
            session.beginTransaction();
            AuthDetails oldAuth = modifyAuthReqMsg.getOldAuthInfo();
            AuthDetails modAuth = modifyAuthReqMsg.getModAuthInfo();
            String oldAttributeValue = oldAuth.getAttributeValue();
            String oldResourceName = oldAuth.getResourceName();
            String oldPermissionName = oldAuth.getPermissionName();
            String oldConstraintName = oldAuth.getConstraintName();
            String attributeValue = modAuth.getAttributeValue();
            String resourceName = modAuth.getResourceName();
            String permissionName = modAuth.getPermissionName();
            String constraintName = modAuth.getConstraintName();
            String constraintValue = modAuth.getConstraintValue();
            mgr.modifyAuthorization(
                    oldAttributeValue, oldResourceName, oldPermissionName,
                    oldConstraintName, attributeValue, resourceName,
                    permissionName, constraintName, constraintValue);
        } catch (AuthZException ex) {
            OSCARSFaultUtils.handleError ( ex, true, session, LOG, method);
        } catch (Exception ex) {
            OSCARSFaultUtils.handleError ( ex, false, session, LOG, method);
        }
        session.getTransaction().commit();
        LOG.debug("modifyAuth.finish");
        return reply;
    }

    /* (non-Javadoc)
     * @see net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType#removeAuth(net.es.oscars.authZ.soap.gen.policy.AuthDetails  removeAuthReqMsg )*
     */
    public EmptyArg removeAuth(AuthDetails removeAuthReqMsg)
            throws OSCARSFaultMessage {

        String method = "authZ/common/PolicyManager:removeAuth";
        LOG.debug("removeAuth.start");
        EmptyArg reply = new EmptyArg();
        PolicyManager mgr = core.getPolicyManager();
        Session session = core.getSession();
        try {
            session.beginTransaction();
            String attributeValue = removeAuthReqMsg.getAttributeValue();
            String resourceName = removeAuthReqMsg.getResourceName();
            String permissionName = removeAuthReqMsg.getPermissionName();
            String constraintName = removeAuthReqMsg.getConstraintName();
            mgr.removeAuthorization(attributeValue, resourceName,
                                    permissionName, constraintName);
        } catch (AuthZException ex) {
            OSCARSFaultUtils.handleError ( ex, true, session, LOG, method);
        } catch (Exception ex) {
            OSCARSFaultUtils.handleError ( ex, false, session, LOG, method);
        }
        session.getTransaction().commit();
        LOG.debug("removeAuth.finish");
        return reply;
    }

    /* (non-Javadoc)
     * @see net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType#addAttr(net.es.oscars.authCommon.soap.gen.AttrDetails  addAttrReqMsg )*
     */
    public EmptyArg addAttr(AttrDetails addAttrReqMsg)
            throws OSCARSFaultMessage {

        String method = "authZ/common/PolicyManager:addAttr";
        LOG.debug("addAttr.start");
        EmptyArg reply = new EmptyArg();
        PolicyManager mgr = core.getPolicyManager();
        Attribute attribute = new Attribute();
        attribute.setAttrId(addAttrReqMsg.getAttrId());
        attribute.setValue(addAttrReqMsg.getValue());
        attribute.setDescription(addAttrReqMsg.getDescription());
        Session session = core.getSession();
        try {
            session.beginTransaction();
            mgr.createAttribute(attribute);
        } catch (AuthZException ex) {
            OSCARSFaultUtils.handleError ( ex, true, session, LOG, method);
        } catch (Exception ex) {
            OSCARSFaultUtils.handleError ( ex, false, session, LOG, method);
        }
        LOG.debug("addAttr.finish");
        return reply;
    }

    /* (non-Javadoc)
     * @see net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType#listAttrs(net.es.oscars.authZ.soap.gen.policy.EmptyArg  listAttrsReqMsg )*
     */
    public ListAttrsReply listAttrs(EmptyArg listAttrsReqMsg)
            throws OSCARSFaultMessage {

        String method = "authZ/common/PolicyManager:listAttrs";
        LOG.debug("listAttrs.start");
        PolicyManager mgr = core.getPolicyManager();
        ListAttrsReply reply = new ListAttrsReply();
        Session session = core.getSession();
        try {
            session.beginTransaction();
            List<Attribute> attributes = mgr.listAttributes();
            for (Attribute attribute: attributes) {
                AttrDetails attrDetails = new AttrDetails();
                attrDetails.setAttrId(attribute.getAttrId());
                attrDetails.setValue(attribute.getValue());
                attrDetails.setDescription(attribute.getDescription());
                reply.getAttribute().add(attrDetails);
            }
        } catch (AuthZException ex) {
            OSCARSFaultUtils.handleError ( ex, true, session, LOG, method);
        } catch (Exception ex) {
            OSCARSFaultUtils.handleError ( ex, false, session, LOG, method);
        }
        session.getTransaction().commit();
        LOG.debug("listAttrs.finish");
        return reply;
    }

    /* (non-Javadoc)
     * @see net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType#modifyAttr(net.es.oscars.authZ.soap.gen.policy.ModifyAttrDetails  modifyAttrReqMsg )*
     */
    public EmptyArg modifyAttr(ModifyAttrDetails modifyAttrReqMsg)
            throws OSCARSFaultMessage {

        String method = "authZ/common/PolicyManager:modifyAttr";
        LOG.debug("modifyAttr.start");
        EmptyArg reply = new EmptyArg();
        PolicyManager mgr = core.getPolicyManager();
        Session session = core.getSession();
        try {
            session.beginTransaction();
            AttrDetails oldReqAttr = modifyAttrReqMsg.getOldAttrInfo();
            AttrDetails modReqAttr = modifyAttrReqMsg.getModAttrInfo();
            Attribute oldAttribute = new Attribute();
            oldAttribute.setAttrId(oldReqAttr.getAttrId());
            oldAttribute.setValue(oldReqAttr.getValue());
            oldAttribute.setDescription(oldReqAttr.getDescription());
            Attribute modAttribute = new Attribute();
            modAttribute.setAttrId(modReqAttr.getAttrId());
            modAttribute.setValue(modReqAttr.getValue());
            modAttribute.setDescription(modReqAttr.getDescription());
            mgr.modifyAttribute(oldAttribute, modAttribute);
        } catch (AuthZException ex) {
            OSCARSFaultUtils.handleError ( ex, true, session, LOG, method);
        } catch (Exception ex) {
            OSCARSFaultUtils.handleError ( ex, false, session, LOG, method);
        }
        session.getTransaction().commit();
        LOG.debug("modifyAttr.finish");
        return reply;
    }

    /* (non-Javadoc)
     * @see net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType#removeAttr(java.lang.String  removeAttrReqMsg )*
     */
    public EmptyArg removeAttr(String attributeValue)
            throws OSCARSFaultMessage {

        String method = "authZ/common/PolicyManager:removeAttr";
        LOG.debug("removeAttr.start");
        EmptyArg reply = new EmptyArg();
        PolicyManager mgr = core.getPolicyManager();
        Session session = core.getSession();
        try {
            session.beginTransaction();
            mgr.removeAttribute(attributeValue);
        } catch (AuthZException ex) {
            OSCARSFaultUtils.handleError ( ex, true, session, LOG, method);
        } catch (Exception ex) {
            OSCARSFaultUtils.handleError ( ex, false, session, LOG, method);
        }
        session.getTransaction().commit();
        LOG.debug("removeAttr.finish");
        return reply;
    }

    /* (non-Javadoc)
     * @see net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType#listResources(net.es.oscars.authZ.soap.gen.policy.EmptyArg  listResourcesReqMsg )*
     */
    public ListResourcesReply listResources(EmptyArg listResourcesReqMsg)
            throws OSCARSFaultMessage {

        String method = "authZ/common/PolicyManager:listResources";
        LOG.debug("listResources.start");
        ListResourcesReply reply = new ListResourcesReply();
        PolicyManager mgr = core.getPolicyManager();
        Session session = core.getSession();
        try {
            session.beginTransaction();
            List<Resource> resources = mgr.listResources();
            for (Resource resource: resources) {
                ResourceDetails resourceDetails = new ResourceDetails();
                resourceDetails.setName(resource.getName());
                resourceDetails.setDescription(resource.getDescription());
                reply.getResource().add(resourceDetails);
            }
        } catch (AuthZException ex) {
            OSCARSFaultUtils.handleError ( ex, true, session, LOG, method);
        } catch (Exception ex) {
            OSCARSFaultUtils.handleError ( ex, false, session, LOG, method);
        }
        session.getTransaction().commit();
        LOG.debug("listResources.finish");
        return reply;
    }

    /* (non-Javadoc)
     * @see net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType#listPermissions(net.es.oscars.authZ.soap.gen.policy.EmptyArg  listPermissionsReqMsg )*
     */
    public ListPermissionsReply listPermissions(EmptyArg listPermissionsReqMsg)
            throws OSCARSFaultMessage {

        String method = "authZ/common/PolicyManager:listPermissions";
        LOG.debug("listPermissions.start");
        ListPermissionsReply reply = new ListPermissionsReply();
        PolicyManager mgr = core.getPolicyManager();
        Session session = core.getSession();
        try {
            session.beginTransaction();
            List<Permission> permissions = mgr.listPermissions();
            for (Permission permission: permissions) {
                PermissionDetails permissionDetails = new PermissionDetails();
                permissionDetails.setName(permission.getName());
                permissionDetails.setDescription(permission.getDescription());
                reply.getPermission().add(permissionDetails);
            }
        } catch (AuthZException ex) {
            OSCARSFaultUtils.handleError ( ex, true, session, LOG, method);
        } catch (Exception ex) {
            OSCARSFaultUtils.handleError ( ex, false, session, LOG, method);
        }
        session.getTransaction().commit();
        LOG.debug("listPermissions.finish");
        return reply;
    }

    /* (non-Javadoc)
     * @see net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType#listConstraints(net.es.oscars.authZ.soap.gen.policy.EmptyArg  listConstraintsReqMsg )*
     */
    public ListConstraintsReply listConstraints(EmptyArg listConstraintsReqMsg)
            throws OSCARSFaultMessage {

        String method = "authZ/common/PolicyManager:listConstraints";
        LOG.debug("listConstraints.start");
        ListConstraintsReply reply = new ListConstraintsReply();
        PolicyManager mgr = core.getPolicyManager();
        Session session = core.getSession();
        try {
            session.beginTransaction();
            List<Constraint> constraints = mgr.listConstraints();
            for (Constraint constraint: constraints) {
                ConstraintDetails constraintDetails = new ConstraintDetails();
                constraintDetails.setName(constraint.getName());
                constraintDetails.setType(constraint.getType());
                constraintDetails.setDescription(constraint.getDescription());
                reply.getConstraint().add(constraintDetails);
            }
        } catch (AuthZException ex) {
            OSCARSFaultUtils.handleError ( ex, true, session, LOG, method);
        } catch (Exception ex) {
            OSCARSFaultUtils.handleError ( ex, false, session, LOG, method);
        }
        session.getTransaction().commit();
        LOG.debug("listConstraints.finish");
        return reply;
    }

    /* (non-Javadoc)
     * @see net.es.oscars.authZ.soap.gen.policy.AuthZPolicyPortType#listRpcs(net.es.oscars.authZ.soap.gen.policy.EmptyArg  listRpcsReqMsg )*
     */
    public ListRpcsReply listRpcs(EmptyArg listRpcsReqMsg)
            throws OSCARSFaultMessage {

        String method = "authZ/common/PolicyManager:listRpcs";
        LOG.debug("listRpcs.start");
        ListRpcsReply reply = new ListRpcsReply();
        PolicyManager mgr = core.getPolicyManager();
        Session session = core.getSession();
        try {
            session.beginTransaction();
            List<Rpc> rpcs = mgr.listRpcs();
            for (Rpc rpc: rpcs) {
                RpcDetails rpcDetails = new RpcDetails();
                rpcDetails.setResourceName(rpc.getResource().getName());
                rpcDetails.setPermissionName(rpc.getPermission().getName());
                rpcDetails.setConstraintName(rpc.getConstraint().getName());
                rpcDetails.setConstraintType(rpc.getConstraint().getType());
                reply.getRpc().add(rpcDetails);
            }
        } catch (AuthZException ex) {
            OSCARSFaultUtils.handleError ( ex, true, session, LOG, method);
        } catch (Exception ex) {
            OSCARSFaultUtils.handleError ( ex, false, session, LOG, method);
        }
        session.getTransaction().commit();
        LOG.debug("listRpcs.finish");
        return reply;
    }

    /**
     * Builds Axis2 AuthDetails class, given Hibernate Authorization bean.
     *
     * @param user A Hibernate Authorization instance
     * @return AuthDetails instance
     */
    private AuthDetails authToAuthReply(Authorization auth) {
        AuthDetails reply = new AuthDetails();
        reply.setAttributeValue(auth.getAttribute().getValue());
        reply.setResourceName(auth.getResource().getName());
        reply.setPermissionName(auth.getPermission().getName());
        reply.setConstraintName(auth.getConstraint().getName());
        reply.setConstraintType(auth.getConstraint().getType());
        reply.setConstraintValue(auth.getConstraintValue());
        return reply;
    }
}
